//
//  CelebrityController.m
//  HB
//
//  Created by 孙智慧 on 16/1/11.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "CelebrityController.h"
#import "CelebrityNetWorkOperation.h"
#import "DJDetailPageController.h"

@implementation CelebrityController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _array = [[NSMutableArray alloc] init];
    
    //设置表数据源
    self.tableView.delegate =self;
    
    self.tableView.dataSource =self;
    
    //取消表格分割线的显示
    self.tableView.separatorStyle=NO;
    
    [self loadCelebrity];
    
    RegisterNotify(NofifyMusicPlay, @selector(stopNavBtnRoation));
}

#pragma mark 加载名人数据
-(void) loadCelebrity
{
    operation= [[CelebrityNetWorkOperation alloc] initWithDelegate:self];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Celebrity" andFuncName:@"CelebrityList"];
    
    NSDictionary * dictInfo = @{@"url":URL};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
}

-(void) opSuccessEx:(id)data opinfo:(NSDictionary *)dictInfo
{
    operation=nil;
    
    //清空数据
    [_array removeAllObjects];
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data2 = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if ([[data2 objectForKey:@"result"] isEqualToString:@"1"])
    {
        
        _array= [data2 objectForKey:@"data"];
        
        //        for (NSInteger i=0; i<[dataArray count]; i++)
        //        {
        //
        //            NSDictionary *dict= [dataArray objectAtIndex:i];
        //
        //            NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
        //
        //            NSString *cid=[dict objectForKey:@"id"];
        //
        //            NSString *title= [dict objectForKey:@"Title"];
        //
        //            NSString *path =[dict objectForKey:@"path"];
        //
        //            NSString *pathName =[dict objectForKey:@"pathName"];
        //
        //            NSString *DJID =[dict objectForKey:@"DJID"];
        //
        //            //NSString *DJName =[dict objectForKey:@"DJName"];
        //
        //            NSString *DJHeadImg =[dict objectForKey:@"DJHeadImg"];
        //
        //            NSString *DJHeadImgName =[dict objectForKey:@"DJHeadImgName"];
        //
        //            NSString *PageTitle =[dict objectForKey:@"PageTitle"];
        //
        //            NSString *PageContent =[dict objectForKey:@"PageContent"];
        //
        //            NSString *ShareURL= [dict objectForKey:@"ShareURL"];
        //
        //            [dic setObject:cid forKey:@"id"];
        //
        //            [dic setObject:title forKey:@"Title"];
        //
        //            [dic setObject:path forKey:@"path"];
        //
        //            [dic setObject:pathName forKey:@"pathName"];
        //
        //            [dic setObject:DJID forKey:@"DJID"];
        //
        //            [dic setObject:DJHeadImg forKey:@"DJHeadImg"];
        //
        //            [dic setObject:DJHeadImgName forKey:@"DJHeadImgName"];
        //
        //            [dic setObject:PageTitle forKey:@"PageTitle"];
        //
        //            [dic setObject:PageContent forKey:@"PageContent"];
        //
        //            [dic setObject:ShareURL forKey:@"ShareURL"];
        //
        //            [_array addObject:dic];
        
//    }
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

}
-(void) stopNavBtnRoation
{
    [self stopNavRightBtnRoation];
    
    [self.marqueeView stopM];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if([self musicPlay].isRoationing)
    {
        self.marqueeView.musicName= [self musicPlay ].playingMusic.name;
        
        [self.marqueeView startM];
    }
    else
    {
        [self.marqueeView stopM];
    }
}

//设置表格有几个数据区域
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}

//设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_array count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 次方法是在拖动表格时 单元格显示在表格的bounds 范围内时触发
    
    if (indexPath.row < [_array count])
    {
        NSString *idefine = @"MingRen";
        
        MingRen *cell = [tableView dequeueReusableCellWithIdentifier:idefine];
        
        if (cell==nil)
        {
            NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:idefine owner:tableView options:nil];
            
            cell = [Objects objectAtIndex:0];
            
        }
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        
        NSDictionary *dict= [_array objectAtIndex:indexPath.row];
        
        cell.dict= dict;
        
        NSString *pathName= [dict objectForKey:@"pathName"];
        
        NSString *imgPath= [dict objectForKey:@"path"];
        
        [Common downLoadImageByURL:cell.myImage andURL:imgPath andName:pathName];
        
        return cell;
    }
    else
    {
        NSString  *cellIdentifier = @"NewsMoreCell";
        
        MoreCell *cell = (MoreCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        //[self  setNextTableData];//加载下一页数据
        if (cell == nil)
        {
            NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
            
            cell = [Objects objectAtIndex:0];
        }
        
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MingRen *cell = (MingRen *)[tableView cellForRowAtIndexPath:indexPath];
    
    DJDetailPageController *djDetailPage=  [self.storyboard instantiateViewControllerWithIdentifier:@"DJDetailPage"];
    
    djDetailPage.dict= cell.dict;
    
    djDetailPage.hidesBottomBarWhenPushed=YES;//隐藏底部菜单栏
    
    [self.navigationController pushViewController:djDetailPage animated:true];
    
}
@end
